package com.leyou.goods.service.impl;

import com.leyou.common.bean.Result;
import com.leyou.common.bean.ResultCode;
import com.leyou.goods.service.GoodsService;
import com.leyou.item.inter.api.IBrandService;
import com.leyou.item.inter.api.ICategoryService;
import com.leyou.item.inter.api.IGoodsService;
import com.leyou.item.inter.api.ISpecGroupParamService;
import com.leyou.item.inter.dto.SpecSelectDTO;
import com.leyou.item.inter.pojo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author zqq
 * @ClassName GoodsServiceImpl
 * @Description
 * @date 2020/3/17-10:12
 */
@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private ICategoryService iCategoryService;

    @Autowired
    private IBrandService iBrandService;

    @Autowired
    private IGoodsService iGoodsService;

    @Autowired
    private ISpecGroupParamService iSpecGroupParamService;

    @Override
    public Result<Map<String, Object>> loadData(Long spuId) {
        Map<String, Object> map = new HashMap<>();
        // 根据id查询spu对象
        Result<Spu> spuResult = this.iGoodsService.querySpuById(spuId);
        if(!spuResult.success()){
            return Result.newFailure(spuResult.getCode(),spuResult.getMessage());
        }
        if(spuResult.getData() == null){
            log.info(ResultCode.GOODS_NULL_FAIL.getMsg());
            return Result.newFailure(ResultCode.GOODS_NULL_FAIL);
        }
        Spu  spu = spuResult.getData();

        // 查询spudetail
        Result<SpuDetail> spuDetailResult = this.iGoodsService.querySpuDetailBySpuId(spuId);
        if(!spuDetailResult.success()){
            return Result.newFailure(spuDetailResult.getCode(),spuDetailResult.getMessage());
        }
        SpuDetail  spuDetail = spuDetailResult.getData();

        // 查询sku集合
        Result<List<Sku>> skusResult = this.iGoodsService.queryEnableSkusBySpuId(spuId);
        if(!skusResult.success()){
            return Result.newFailure(skusResult.getCode(),skusResult.getMessage());
        }
        List<Sku>  skus = skusResult.getData();

        // 查询分类
        List<Long> cids = Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3());
        Result<List<String>> namesResult = this.iCategoryService.queryNamesByIds(cids);
        if(!namesResult.success()){
            return Result.newFailure(namesResult.getCode(),namesResult.getMessage());
        }
        List<String>  names = namesResult.getData();
        List<Map<String, Object>> categories = new ArrayList<>();
        Map<String, Object> categoryMap = null;
        for (int i = 0; i < cids.size(); i++) {
            categoryMap = new HashMap<>();
            categoryMap.put("id", cids.get(i));
            categoryMap.put("name", names.get(i));
            categories.add(categoryMap);
        }

        // 查询品牌
        Result<Brand> brandResult = this.iBrandService.queryBrandById(spu.getBrandId());
        if(!brandResult.success()){
            return Result.newFailure(brandResult.getCode(),brandResult.getMessage());
        }
        Brand  brand = brandResult.getData();

        // 查询规格参数组
        Result<List<SpecGroup>> groupsResult = this.iSpecGroupParamService.queryGroupsWithParam(spu.getCid3());
        if(!groupsResult.success()){
            return Result.newFailure(groupsResult.getCode(),groupsResult.getMessage());
        }
        List<SpecGroup>  groups = groupsResult.getData();

        // 查询特殊的规格参数
        SpecSelectDTO dto = new SpecSelectDTO(null, spu.getCid3(), null, null);
        Result<List<SpecParam>> paramsResult = this.iSpecGroupParamService.queryParams(dto);
        if(!paramsResult.success()){
            return Result.newFailure(paramsResult.getCode(),paramsResult.getMessage());
        }
        List<SpecParam>  params = paramsResult.getData();
        Map<Long, String> paramMap = new HashMap<>();
        params.forEach(param -> {
            paramMap.put(param.getId(), param.getName());
        });

        // 封装spu
        map.put("spu", spu);
        // 封装spuDetail
        map.put("spuDetail", spuDetail);
        // 封装sku集合
        map.put("skus", skus);
        // 分类
        map.put("categories", categories);
        // 品牌
        map.put("brand", brand);
        // 规格参数组
        map.put("groups", groups);
        // 查询特殊规格参数
        map.put("params", paramMap);
        return Result.newSuccess(map);
    }
}
